package com.gl.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gl.entity.UcenterMember;
import com.gl.service.UcenterMemberService;
import com.gl.utils.CommonResponse;
import com.gl.utils.JwtUtils;
import com.gl.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-12-14
 */
@RestController
@RequestMapping("/member")
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService memberService;

    //登录    返回jwt规则生成封装用户信息的token字符串
    @PostMapping("login")
    public String login(@RequestBody UcenterMember member){
        String token = memberService.login(member);
        return token;
    }

    //根据token获取用户信息
    @GetMapping("getUserInfoByToken")
    public UcenterMember getUserInfoByToken(HttpServletRequest request){
        //根据request头信息中的token字符串，通过JWT工具类获取用户id
        String id = JwtUtils.getMemberIdByJwtToken(request);
        //根据id获取用户信息
        UcenterMember member = memberService.getById(id);
        return member;
    }

    //注册
    @PostMapping("register")
    public String register(@RequestBody UcenterMember member){
        member.setPassword(MD5.encrypt(member.getPassword()));//密码加密
        member.setAvatar("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2456468987,3284231714&fm=26&gp=0.jpg");//设置注册用户默认图片
        memberService.save(member);
        return "ok";
    }





}

